#include "stdafx.h"
#include "main_data_c.h"
#include "textInput.h"


bool onMsgChar(WPARAM wParam, LPARAM lParam)
{
    int& strLen = mainData.strBufDataSize_7BA8558[mainData.strBufIndex_7C41CF8];

    if (!mainData.byte_54CA6B1)
        mainData.byte_7C41C7C[4 * mainData.strBufIndex_7C41CF8] = 0;
    if (wParam == VK_BACK)
    {
        int charLen = 0;
        int count = 0;
        while (count < strLen)
        {
            charLen = _mbclen((const unsigned char*)mainData.strBuf_7BE8BC0[mainData.strBufIndex_7C41CF8] + count);
            count += charLen;
        }
        strLen -= charLen;
        if (strLen < 0) strLen = 0;
        mainData.strBuf_7BE8BC0[mainData.strBufIndex_7C41CF8][strLen] = 0;
        if (charLen == 2)
            mainData.strBuf_7BE8BC0[mainData.strBufIndex_7C41CF8][strLen + 1] = 0;
    }
    else if (wParam == VK_TAB)
    {
        if (byte_47CEB8)
        {
            if (dword_47CEBC <= 1)
            {
                if (!strLen)
                    mainData.byte_7C41CF0 = mainData.byte_7C41CF0 == 0;
            }
            else
            {
                mainData.strBufIndex_7C41CF8 = (mainData.strBufIndex_7C41CF8 + 1) % dword_47CEBC;
                sub_401570(25, 0, 0);
            }
        }
    }
    else
    {
        if (wParam != VK_RETURN)
        {
            if (byte_47CEB8 || mainData.byte_7C41CF0)
            {
                if (!mainData.byte_7C41CF1)
                {
                LABEL_47:
                    mainData.dword_54CA6C8--;
                    if (mainData.dword_54CA6C8 <= 0 && (wParam & 0x80) == 0x80)
                    {
                        mainData.dword_54CA6C8 = 2;
                    }
                    if (mainData.byte_54CA6C5)
                    {
                        mainData.byte_54CA6C5 = 0;
                    }
                    else if (mainData.dword_54CA6C8 != 2 || strLen != strBufSizeLimit_47CEC8[mainData.strBufIndex_7C41CF8] - 1)
                    {
                        if (strLen < strBufSizeLimit_47CEC8[mainData.strBufIndex_7C41CF8])
                        {
                            mainData.strBuf_7BE8BC0[mainData.strBufIndex_7C41CF8][strLen] = wParam;
                            strLen++;
                            if (strLen > 255)
                                strLen = 255;
                            if (mainData.strBufIndex_7C41CF8 == dword_47CEC4)
                            {
                                if ((BYTE)wParam == VK_INSERT)
                                {
                                    strLen--;
                                    mainData.strBuf_7BE8BC0[mainData.strBufIndex_7C41CF8][strLen] = 0;
                                }
                                if (strLen == 6)
                                {
                                    mainData.strBuf_7BE8BC0[mainData.strBufIndex_7C41CF8][6] = 45;
                                    strLen = 7;
                                }
                            }
                        }
                    }
                    else
                    {
                        mainData.byte_54CA6C5 = 1;
                    }
                    return false;
                }
            }
            else if (!mainData.byte_7C41CF1)
            {
                if (!mainData.byte_7C41CEF && mainData.dword_81C498C != 126)
                    return false;
                goto LABEL_47;
            }
            if ((char)wParam < '0' || (char)wParam > '9')
                return false;
            goto LABEL_47;
        }
        if (nScene_482904 == 5 && !mainData.byte_7C41CEF && mainData.dword_81C498C != 126)
        {
            if (strLen || mainData.byte_7C41CF0)
            {
                mainData.strBufIndex_7C41CF8 = 0;
                if (mainData.byte_7C41CF1)
                {
                    mainData.dword_7C41CF4 = atoi(mainData.strBuf_7BE8BC0[0]);
                }
                else if (!handleCmdText(mainData.strBuf_7BE8BC0[0]))
                {
                    if (speakFilter(mainData.strBuf_7BE8BC0[mainData.strBufIndex_7C41CF8]))
                    {
                        strcpy(mainData.strBuf_7BE8BC0[mainData.strBufIndex_7C41CF8], mainData.textBmd_7B7BCE8[570]);
                    }
                    if (mainData.strBufDataSize_7BA8558[1] && byte_47CE3C)
                    {
                        Send_02_406030((char*)mainData.strBuf_7BE8BC0[1], mainData.strBuf_7BE8BC0[mainData.strBufIndex_7C41CF8]);
                        gamePrintLog(
                            (const char*)(mainData.dword_79DBE88 + 380),
                            mainData.strBuf_7BE8BC0[mainData.strBufIndex_7C41CF8],
                            0);
                    }
                    else
                    {
                        if (*(WORD*)(mainData.dword_79DBE88 + 588) != 560
                            || *(mainData.dword_79DBE88 + 730))
                        {
                            handleActionText(mainData.strBuf_7BE8BC0[mainData.strBufIndex_7C41CF8]);
                        }
                        Send_00_405EC0(mainData.strBuf_7BE8BC0[mainData.strBufIndex_7C41CF8]);
                    }
                    dword_47CF00 = 114;
                }
                byte_47CEB8 = 0;
                memset(mainData.strBuf_7BE8BC0[mainData.strBufIndex_7C41CF8], 0, 0x100u);
                strLen = 0;
            }
            else if (byte_47CEB8)
            {
                byte_47CEB8 = 0;
            }
            else
            {
                strBufSizeLimit_47CEC8[0] = 42;
                mainData.dword_7C41368 = 0;
                dword_47CEBC = 2;
                mainData.byte_7C41CF1 = 0;
                byte_47CEB8 = 1;
            }
        }
    }
    return true;
}

